//BAO TAM VO
#include "my_malloc.h"
#include <stdio.h>
#include <string.h>
int *a;
int *b;
int *c;
int *d;
int *e;

int main() {
	
	/*
	 * Test code goes here
	 */
	printf("\nfreelist in the beginning should have no block available: ");
	print_freelist();
	a = my_malloc(245);
	printf("\na = my_malloc(245) should return the first block at index 5: ");
	printf("\nactualsize of the block is : %ld", 245 + sizeof(metadata_t));
	printf("\n the block returned is %ld", (long)a - sizeof(metadata_t));
	print_freelist();

	b = my_malloc(1);
	printf("\nb = my_malloc(1) should return the first block at index 1: ");
	printf("\nactualsize of the block is : %ld", 1 + sizeof(metadata_t));
	printf("\n the block returned is %ld", (long)2024 - sizeof(metadata_t));
	print_freelist();

	c = my_malloc(2024);
	printf("\nc = my_malloc(2024) should return the first block at index 7: ");
	printf("\nactualsize of the block is : %ld", 2024 + sizeof(metadata_t));
	printf("\n the block returned is %ld", (long)c - sizeof(metadata_t));
	print_freelist();
	d = my_malloc(2024);
	printf("\nd = my_malloc(2024) should return the first block at index 7: ");
	printf("\nactualsize of the block is : %ld", 2024 + sizeof(metadata_t));
	printf("\n the block returned is %ld", (long)d - sizeof(metadata_t));
	print_freelist();

	e = my_malloc(230);
	printf("\ne = my_malloc(230) should return the first block at index 4: ");
	printf("\nactualsize of the block is : %ld", 230 + sizeof(metadata_t));
	printf("\n the block returned is %ld", (long)e - sizeof(metadata_t));
	print_freelist();
	printf("\n\nfree b, its block is at %ld",(long)b - sizeof(metadata_t));
	my_free(b);
	print_freelist();

	printf("\n\nfree a, its block is at %ld",(long)a - sizeof(metadata_t));
	my_free(a);
	print_freelist();
	printf("\nmalloc 2024 for a");

	a = my_malloc(2024);
	printf("\nmalloc 2048 for b");
	b = my_malloc(2048);

	printf("\nb is not supposed to be malloc'd %ld %ld %ld %ld",(long)a,(long)b,(long)c,(long)d);
	printf("\nERRNO is : %d", (int)ERRNO);
	print_freelist();
	my_free(e);
	printf("\n\nfree e");
	print_freelist();
	printf("\n\ndouble free e");
	my_free(e);
	printf("\nERRNO is: %d", (int)ERRNO);

	printf("\nmalloc b, e with 2024, should throw out of memory on e");
	b = my_malloc(2024);
	e = my_malloc(2024);
	printf("\nERRNO is: %d", (int)ERRNO);
	print_freelist();

	char str[60] = "memmove is here to move this tring";
	printf("\nmemmove: %s", str);
	my_memmove(str+20,str+15,11);
	printf("\nmy_memmove(str+20,str+15,11): %s", str);

	printf("\n");
	return 0;
}
